<?php

//打印
function AAA($data)
{
	echo '<pre>';
	print_r($data);
	echo '</pre>';
	exit;
	
}

//usort排序函数
function multiFormPicSort($a,$b){
	if ($a['sort'] == $b['sort']) return 0;
	return ($a['sort'] > $b['sort']) ? 1 : -1;
}

define("CHARSET",'utf-8');
//字符串切割（需要定义常量 CHARSET）
function str_cut($string, $length, $dot = '..') {
	$strlen = strlen($string);
	if($strlen <= $length) return $string;
	
	$string = str_replace(array(' ','&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array('∵',' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
	$strcut = '';
	if(strtolower(CHARSET) == 'utf-8') {
		$length = intval($length-strlen($dot)-$length/3);
		$n = $tn = $noc = 0;
		while($n < strlen($string)) {
			$t = ord($string[$n]);
			if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1; $n++; $noc++;
			} elseif(194 <= $t && $t <= 223) {
				$tn = 2; $n += 2; $noc += 2;
			} elseif(224 <= $t && $t <= 239) {
				$tn = 3; $n += 3; $noc += 2;
			} elseif(240 <= $t && $t <= 247) {
				$tn = 4; $n += 4; $noc += 2;
			} elseif(248 <= $t && $t <= 251) {
				$tn = 5; $n += 5; $noc += 2;
			} elseif($t == 252 || $t == 253) {
				$tn = 6; $n += 6; $noc += 2;
			} else {
				$n++;
			}
			if($noc >= $length) {
				break;
			}
		}
		if($noc > $length) {
			$n -= $tn;
		}
		$strcut = substr($string, 0, $n);
		$strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);
	} else {
		$dotlen = strlen($dot);
		$maxi = $length - $dotlen - 1;
		$current_str = '';
		$search_arr = array('&',' ', '"', "'", '“', '”', '—', '<', '>', '·', '…','∵');
		$replace_arr = array('&amp;','&nbsp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;',' ');
		$search_flip = array_flip($search_arr);
		for ($i = 0; $i < $maxi; $i++) {
			$current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
			if (in_array($current_str, $search_arr)) {
				$key = $search_flip[$current_str];
				$current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
			}
			$strcut .= $current_str;
		}
	}
	return $strcut.$dot;
}

function getFullTime($time){
		
		
	if(strlen($time)<=1){
		
		return '0'.$time;
	}else{
		
		return $time;
	}
}

function downloads($inDir){
	$dir=iconv('utf-8','gbk',$inDir);
	//$dir='F:/22/wamp';
	$dirArr=explode('/',$dir);
	$fileName=iconv('gbk','utf-8',$dirArr[count($dirArr)-1]);
	 
	if (!file_exists($dir)){
		header("Content-type: text/html; charset=utf-8");
		echo "File not found!";
		exit; 
	} else {
		$file = fopen($dir,"r"); 
		Header("Content-type: application/octet-stream");
		Header("Accept-Ranges: bytes");
		Header("Accept-Length: ".filesize($dir));
		Header("Content-Disposition: attachment; filename=".$fileName);
		echo fread($file, filesize($dir));
		fclose($file);
	}
}


function getAppInfoByK($key){
	
	$APPS = $GLOBALS['APPS']; 
	
	foreach($APPS as $val){
		
		if($val['k']==$key){
			
			return $val;
		}
	}
	return $APPS[0];
}


function getAppAccessKey($key){
	
	$APPS = $GLOBALS['APPS']; 
	
	foreach($APPS as $val){
		
		if($app['k']==$k){
			
			return $val['accessKey'];
		}
	}
	return $APPS[0]['accessKey'];
}


function sendmail($tomail,$title,$content){

		require_once('phpmailer.php');

		
		
		$mainTitle = 'P4P神车有一个新用户,请及时跟进！';
		
		

			

			//ibelceramics@163.com
        /*邮件设置信息*/
        $email_set = array(

		   'Host'=> "smtp.126.com",
		   'Port'=>'25',
		   'email_user'=>'huangchengtian78',
		   'email_pwd'=>'22639639',
		   'email'=>'huangchengtian78@126.com',

		   'email_name'=>'这是一个邮件'

		  

		   

		);

            
        $mail = new PHPMailer(true); //实例化PHPMailer类,true表示出现错误时抛出异常
       
        $mail->IsSMTP(); // 使用SMTP

          //$mail->CharSet ="UTF-8";//设定邮件编码
		  $mail->CharSet ="GBK";//设定邮件编码
          $mail->Host       = $email_set['Host']; // SMTP server
          $mail->SMTPDebug  = 1;                     // 启用SMTP调试 1 = errors  2 =  messages
          $mail->SMTPAuth   = true;                  // 服务器需要验证
          $mail->Port       = $email_set['Port'];                    // 设置端口
		  //$mail->FromName   =  "管理员";
		  //$mail->AddAttachment("./fujian/333.zip"); 
         // $mail->SMTPSecure = "ssl";    
            /*
            $mail->SMTPSecure = "ssl";                
            $mail->Host       = "smtp.gmail.com";    
            $mail->Port       = 465;                 
            */
       
          $mail->Username   = $email_set['email_user']; //SMTP服务器的用户帐号
          $mail->Password   = $email_set['email_pwd'];       //SMTP服务器的用户密码
          $mail->AddReplyTo($email_set['email'],$email_set['email_name']); //收件人回复时回复到此邮箱,可以多次执行该方法
          if (is_array($tomail)){
              foreach ($tomail as $m){
                   $mail->AddAddress($m, 'myuser');
              }
          }else{
              $mail->AddAddress($tomail, 'myuser');
          }
        
          $mail->SetFrom($email_set['email'],$email_set['email_name']);
          $mail->Subject = $title;
       
          //以下是邮件内容相关
          $mail->Body = $content;
          $mail->IsHTML(true);
       
          //$body = file_get_contents('tpl.html'); //获取html网页内容
         // $mail->MsgHTML(eregi_replace("[]",'',$body));
        return $mail->Send()? true:false;

}


function isWeixin() { 
  if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { 
    return true; 
  } else {
    return false; 
  }
}

function httpGet($url){  
    $curl = curl_init();  
    curl_setopt($curl, CURLOPT_URL, $url);  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    $res=json_decode(curl_exec($curl),true);  
    curl_close($curl);  
    return $res;  
}  


function httpPost($url,$post_data){    
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    // post数据  
    curl_setopt($ch, CURLOPT_POST, 1);  
    // post的变量  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
    $output=json_decode(curl_exec($ch),true);  
    curl_close($ch);  
    return $data;  
}  


function httpsGet($url){  
    $curl = curl_init();  
    curl_setopt($curl, CURLOPT_URL, $url);  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// https请求不验证证书和hosts  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  
    $res=json_decode(curl_exec($curl),true);  
    curl_close($curl);  
    return $res;  
}  


function httpsPost($url,$post_data){  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    // post数据  
    curl_setopt($ch, CURLOPT_POST, 1);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  
    // post的变量  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  
    $output=json_decode(curl_exec($ch),true);  
    curl_close($ch);  
    return $output;  
}

function getAuth($node){
	
	$loginInfo = $_SESSION['loginInfo'];
	
	$access = M('admin_access')->where('role_id='.$loginInfo['group_id'])->select();
	
	$allNodes = array();
	
	foreach($access as $val){
		
		$row = M('admin_node')->where('id='.$val['node_id'])->find();
		
		if(!empty($row)){
			
			$allNodes[]= $row;
		}
	}
	//AAA($allNodes);
	$flag = false;
	
	foreach($allNodes as $nodeRow){
		
		if($nodeRow['name']==$node){
			
			$flag = true;
		}
	}
	
	if(!$flag){
		
		return 'style="display:none;"';
	}else{
		
		return '';
	}
}


function generate_code($length = 4) {
	return rand(pow(10,($length-1)), pow(10,$length)-1);
}

function getFullNum($student_id = 0) {
	
	$stuInfo = M('student')->where('student_id='.$student_id)->find();
	
	$prefix  = '';
	
	if($stuInfo['add_class']==1){
		
		$prefix.='T';
	}
	
	if($stuInfo['add_class']==2){
		
		$prefix.='X';
	}
	
	if($stuInfo['add_class']==3){
		
		$prefix.='Z';
	}
	
	if($stuInfo['add_class']==4){
		
		$prefix.='D';
	}
	
	if($stuInfo['volunteer']==1){
		
		$prefix.='P';
	}
	
	if($stuInfo['volunteer']==2){
		
		$prefix.='G';
	}
	
	$num = $stuInfo['student_id'];
	
	$str =  '';
	
	if($num<1000){
		
		$str = '0'.$num;
	}
	
	if($num<100){
		
		$str = '00'.$num;
	}
	
	if($num<10){
		
		$str = '000'.$num;
	}
	
	
	

	
	return $prefix.$str;
}


function findNum($str=''){
    $str=trim($str);
    if(empty($str)){return '';}
    $result='';
    for($i=0;$i<strlen($str);$i++){
        if(is_numeric($str[$i])){
            $result.=$str[$i];
        }
    }
    return $result;
}


